package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/model"
)

type AdminRoleRelationDAO struct {
	bizID int64
}

type AdminRoleRelationListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func newAdminRoleRelationDAO(bizID int64) *AdminRoleRelationDAO {
	return &AdminRoleRelationDAO{
		bizID: bizID,
	}
}

func (c *AdminRoleRelationDAO) MGetAdminRoleRelation(ctx context.Context, ids []int64) (map[int64]*model.AdminRoleRelation, error) {
	list, err := AdminRoleRelation.WithContext(ctx).Where(

		AdminRoleRelation.ID.In(ids...),
		AdminRoleRelation.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGetAdminRoleRelation err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.AdminRoleRelation)
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}

func (c *AdminRoleRelationDAO) GetAdminRoleRelationByConditions(ctx context.Context, condition []gen.Condition) (*model.AdminRoleRelation, error) {
	tx := AdminRoleRelation.WithContext(ctx).Where(

		AdminRoleRelation.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdminRoleRelationByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}

func (c *AdminRoleRelationDAO) GetAdminRoleRelationList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.AdminRoleRelation, error) {
	tx := AdminRoleRelation.WithContext(ctx).Where(AdminRoleRelation.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdminRoleRelationList err: %v", err)
		return nil, err
	}
	return list, nil
}

func (c *AdminRoleRelationDAO) GetAdminRoleRelationTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := AdminRoleRelation.WithContext(ctx).Where(AdminRoleRelation.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdminRoleRelationTotal error: %v", err)
		return 0, err
	}
	return total, nil
}

func (c *AdminRoleRelationDAO) GetAdminRoleRelationInfoByID(ctx context.Context, id int64) (*model.AdminRoleRelation, error) {
	info, err := AdminRoleRelation.WithContext(ctx).Where(AdminRoleRelation.DeletedAt.IsNull()).Where(AdminRoleRelation.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdminRoleRelationInfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}

func (c *AdminRoleRelationDAO) GetListWithTotal(ctx context.Context, params *AdminRoleRelationListParams) ([]*model.AdminRoleRelation, int64, error) {
	tx := AdminRoleRelation.WithContext(ctx).Where(AdminRoleRelation.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.AdminRoleRelation.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}

func (c *AdminRoleRelationDAO) CreateAdminRoleRelation(ctx context.Context, data *model.AdminRoleRelation) (*model.AdminRoleRelation, error) {
	if err := AdminRoleRelation.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateAdminRoleRelation error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdminRoleRelationDAO) CreateAdminRoleRelationWithTx(ctx context.Context, tx *Query, data *model.AdminRoleRelation) (*model.AdminRoleRelation, error) {
	if err := tx.AdminRoleRelation.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateAdminRoleRelation error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdminRoleRelationDAO) BatchCreateAdminRoleRelation(ctx context.Context, data []*model.AdminRoleRelation) error {
	if err := AdminRoleRelation.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatchesAdminRoleRelation error: %v", err)
		return err
	}
	return nil
}

func (c *AdminRoleRelationDAO) BatchCreateAdminRoleRelationWithTx(ctx context.Context, tx *Query, data []*model.AdminRoleRelation) ([]*model.AdminRoleRelation, error) {
	if err := tx.AdminRoleRelation.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTxAdminRoleRelation error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdminRoleRelationDAO) UpdateAdminRoleRelation(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := AdminRoleRelation.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateAdminRoleRelation error: %v", err)
		return err
	}
	return nil
}

func (c *AdminRoleRelationDAO) UpdateAdminRoleRelationWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.AdminRoleRelation.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateAdminRoleRelation error: %v", err)
		return err
	}
	return nil
}

func (c *AdminRoleRelationDAO) DeleteAdminRoleRelation(ctx context.Context, condition []gen.Condition) error {
	_, err := AdminRoleRelation.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteAdminRoleRelation error: %v", err)
		return err
	}
	return nil
}

func (c *AdminRoleRelationDAO) DeleteAdminRoleRelationWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.AdminRoleRelation.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteAdminRoleRelationWithTx error: %v", err)
		return err
	}
	return nil
}
